package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.ProductRequestParam;
import com.clever.mall.commons.dto.SearchRequestParam;
import com.clever.mall.entity.Brand;
import com.clever.mall.entity.Product;
import com.clever.mall.entity.Search;
import com.clever.mall.entity.User;
import com.clever.mall.mapper.BrandMapper;
import com.clever.mall.mapper.ProductMapper;
import com.clever.mall.mapper.UserMapper;
import com.clever.mall.service.CateGoryService;
import com.clever.mall.service.ProductService;
import com.clever.mall.service.SearchService;
import com.clever.mall.utils.MyBeanUtils;
import com.clever.mall.vo.BrandVo;
import com.clever.mall.vo.ProductVo;
import com.clever.mall.vo.SearchResult;
import com.clever.mall.vo.UserVo;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    CateGoryService cateGoryService;
    @Resource
    SearchService searchService;
    @Resource
    BrandMapper brandMapper;

    //查询热门商品的前8条
    @Override
    public List<ProductVo> findHeatProduct() {
        List<Product> products = productMapper.selectHeatProduct();
        List<ProductVo> productVos = MyBeanUtils.copyListProperties(products, ProductVo::new);
        return productVos;
    }

    ////以id查找商品详情
    @Override
    public ProductVo findById(int id) {
        //查询商品数据
        Product product = productMapper.selectById(id);
        User user = new User();
        //对应的卖家信息
        user.setUserId(product.getUserId());
        User user1 = userMapper.selectUserByUser(user);
        //对应的品牌信息
        Brand brand = brandMapper.selectById(product.getBrandId());
        ProductVo productVo = new ProductVo();
        UserVo userVo = new UserVo();
        BrandVo brandVo = new BrandVo();
        BeanUtils.copyProperties(product, productVo);
        BeanUtils.copyProperties(user1, userVo);
        BeanUtils.copyProperties(brand, brandVo);
        productVo.setUserVo(userVo);
        productVo.setBrandVo(brandVo);
        return productVo;
    }

    //以品牌id查询所有的该品牌下的商品
    @Override
    public List<ProductVo> findAllByBrandId(int id) {
        List<Product> products = productMapper.selectAllByBrandId(id);
        return MyBeanUtils.copyListProperties(products, ProductVo::new);
    }

    //以品牌id查询此品牌中的热门商品前4条
    @Override
    public List<ProductVo> findHeatByBrandId(int id) {
        List<Product> products = productMapper.selectHeatByBrandId(id);
        return MyBeanUtils.copyListProperties(products, ProductVo::new);
    }

    //以分裂id查询该分类的商品
    @Override
    public List<ProductVo> findAllByCateGoryId(int id) {
        List<Product> products = productMapper.selectAllByCateGoryId(id);
        return MyBeanUtils.copyListProperties(products, ProductVo::new);
    }

    //以关键字查模糊匹配询商品和用户
    @Override
    public SearchResult findProductsAndUserByKeyword(String keyword) {
        //匹配上用户名的用户
/*        User user = new User();
        user.setUserName(keyword);
        User user1 = userMapper.selectUserByUser(user);*/
        UserVo userVo = new UserVo();
        //BeanUtils.copyProperties(user1, userVo);
        List<UserVo> userVos = new ArrayList<>();
        userVos.add(userVo);

        //匹配上商品名的商品
        List<ProductVo> productVos = findByConditions(new ProductRequestParam(keyword));
        return new SearchResult(productVos, userVos);
    }

    //多条件对商品的过滤查询1.是否上新，价格区间，分类，几成新，尺码
    @Override
    public List<ProductVo> findByConditions(ProductRequestParam requestParam) {
        //前端默认分类为一级分类
        Integer firstCategoryId = requestParam.getFirstCategoryId();
        if (firstCategoryId != null) {
            //判断是否为一级id,如果不是就赋值给二级分类
            boolean flag = cateGoryService.isFirst(firstCategoryId);
            if (!flag) {
                requestParam.setSecondCategoryId(firstCategoryId);
                requestParam.setFirstCategoryId(null);
            }
        }
        //多条件查询结果
        List<Product> products = productMapper.selectByConditions(requestParam);
        List<ProductVo> productVos = MyBeanUtils.copyListProperties(products, ProductVo::new);

        //将查询的关键字存入Search表，1.已存在次数+1 2.不存在新增
        String word = requestParam.getName();
        if (word != null) {
            //搜索词表中查询记录
            Search search = searchService.findByWord(word);
            if (search != null) {
                //是对象的数量字段加1在更新
                search.setCount(search.getCount() + 1);
                searchService.modifyCount(search);
            } else {
                searchService.addWords(new SearchRequestParam(word, 1));
            }
        }
        return productVos;
    }

    //活动id查询参与此活动的商品
    @Override
    public List<ProductVo> findByActId(int actId) {
        List<Product> products = productMapper.selectByActId(actId);
        List<ProductVo> productVos = MyBeanUtils.copyListProperties(products, ProductVo::new);
        return productVos;
    }

}
